package util

type AllowMapKey interface {
	~int | ~int16 | ~int32 | ~int64 |
		~uint | ~uint16 | ~uint32 | ~uint64 |
		~string
}

// 对Map成员进行遍历并返回新的数组
func MapMap[ROW any, KEY AllowMapKey](m map[KEY]ROW, callback func(row ROW, key KEY) ROW) map[KEY]ROW {
	ret := make(map[KEY]ROW)
	for key, row := range m {
		ret[key] = callback(row, key)
	}

	return ret
}

// 对Map成员进行遍历(修改原始值)
func MapMapRef[ROW any, KEY AllowMapKey](m map[KEY]ROW, callback func(row ROW, key KEY) ROW) map[KEY]ROW {
	for key, row := range m {
		m[key] = callback(row, key)
	}

	return m
}

// 将Map中的值转成数组
func MapValues[ROW any, KEY AllowMapKey](m map[KEY]ROW) []ROW {
	ret := make([]ROW, 0)
	for _, row := range m {
		ret = append(ret, row)
	}

	return ret
}

// 将Map中key值转成数组
func MapKeys[ROW any, KEY AllowMapKey](m map[KEY]ROW) []KEY {
	ret := make([]KEY, 0)
	for k := range m {
		ret = append(ret, k)
	}

	return ret
}